# -*- coding: utf-8 -*-
import threading
from tencentcloud.common import credential
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
# 导入对应产品模块的client models。
from tencentcloud.sms.v20210111 import sms_client, models

# 导入可选配置类
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from enum import Enum


class MessageTemplate(Enum):
    LOGIN_AUTHCODE =  "2126621"


class MessageSender:
    def __init__(self) -> None:
        self.client = sms_client.SmsClient(self.cred, "ap-guangzhou", self.clientProfile)

    @property
    def cred(self):
        return credential.Credential("AKIDtkaUUajn4IXrBbyS6gHxjbceccaUJ1gT", "65ynr4Ng6Wr8RqNrKtfZhr6V1iYWVZen")

    @property
    def httpProfile(self):
        httpProfile = HttpProfile()
        # 如果需要指定proxy访问接口，可以按照如下方式初始化hp（无需要直接忽略）
        # httpProfile = HttpProfile(proxy="http://用户名:密码@代理IP:代理端口")
        httpProfile.reqMethod = "POST"  # post请求(默认为post请求)
        httpProfile.reqTimeout = 30    # 请求超时时间，单位为秒(默认60秒)
        httpProfile.endpoint = "sms.tencentcloudapi.com"  # 指定接入地域域名(默认就近接入)
        return httpProfile
    
    @property
    def clientProfile(self):
        clientProfile = ClientProfile()
        clientProfile.signMethod = "TC3-HMAC-SHA256"  # 指定签名算法
        clientProfile.language = "en-US"
        clientProfile.httpProfile = self.httpProfile
        return clientProfile

    @property
    def req(self):
        req = models.SendSmsRequest()
        req.SignName = "建水县布零布零软件"
        req.SmsSdkAppId = "1400900708"
        return req

    def send(self, templateID, templatePara, phoneNum):
        req = self.req
        req.TemplateId = templateID
        
        req.TemplateParamSet = [templatePara, "5"]
        # 下发手机号码，采用 E.164 标准，+[国家或地区码][手机号]
        # 示例如：+8613711112222， 其中前面有一个+号 ，86为国家码，13711112222为手机号，最多不要超过200个手机号
        req.PhoneNumberSet = [f"+86{phoneNum}"]
        threading.Thread(target=self.client.SendSms, args=(req,), daemon=True).start()


if __name__ == "__main__":
    MessageSender().send(MessageTemplate.LOGIN_AUTHCODE.value, "1234", 15208461203)